import {getAllDocBySiteName} from 'network/document'

export default {
  state: {
    documents: [],
    cargos: []
  },
  mutations: {
    setDocuments(state,payload) {
      state.documents = payload
    },
    setCargos(state,payload) {
      state.cargos.splice(0)
      for(let doc of payload) {
        for(let cargo of doc.cargos) {
          state.cargos.push({
            cargoId: cargo.cargoId,
            docId: cargo.docId,
            cargoName: cargo.cargoName,
            cargoPack: cargo.cargoPack,
            num: cargo.num,
            bulk: cargo.bulk,
            weight: cargo.weight,
            price: cargo.price,
            endSite: doc.docEnd,
            state: doc.state
          })
        }
      }
    }
  },
  actions: {
    setDocument({commit},name) {
      getAllDocBySiteName(name).then(res => {
        commit('setDocuments',res)
        commit('setCargos',res)
      }).catch(err => {
        console.log(err)
      })
    }
  }
}